
import { Logger } from "../../engine/Logger";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { GameConfig } from "../config/GameConfig";
import { RoomConfig } from "../config/RoomConfig";
import RoomConfig_Mj from "../config/RoomConfig_Mj";
import { ViewConfig } from "../config/ViewConfig";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";
import mj_create from "./createroom/mj_create";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewCreateRoom extends ViewBase {

    private btnGameList: Array<cc.Node> = []
    private gameType: number = GameConfig.Game_Type.Mj;

    private mj_create: mj_create


    onEnable(): void {
        super.onEnable();
        // for (let i = 0; i < GameConfig.Game_Num; i++) {
        //     this.btnGameList[i] = this.findChild(this.node, "btnGame" + i)
        //     UIHelper.onAddEvent(this.btnGameList[i], () => {
        //         this.gameType = i + 1;
        //         this.refresh()
        //     })
        // }
        this.mj_create = this.getChild(this.node, "mj_create", mj_create)

        let btnSure: cc.Node = this.findChild(this.node, "btnSure")
        UIHelper.onAddEvent(btnSure, () => {
            this.onClickSure()
        })
        this.refresh()
    }

    private refresh() {
        for (let i = 0; i < this.btnGameList.length; i++) {
            if (this.gameType == (i + 1)) {
                this.btnGameList[i].getComponent(cc.Sprite).spriteFrame = this.btnGameList[i].getComponent(SpriteFrameComponent).spriteFrameList[1]
            } else {
                this.btnGameList[i].getComponent(cc.Sprite).spriteFrame = this.btnGameList[i].getComponent(SpriteFrameComponent).spriteFrameList[0]
            }
        }
        this.mj_create.node.active = false

        if (this.gameType == GameConfig.Game_Type.Mj) {
            this.mj_create.node.active = true
        }


    }

    private onClickSure() {
        let roomConfig: RoomConfig_Mj;
        if (this.gameType == GameConfig.Game_Type.Mj) {
            roomConfig = this.mj_create.getRoomConfig()
        }
        if (UserModel.instance.nowClub) {
            // roomConfig.cost_type = RoomConfig.cost_type.quanzhu
            Logger.log("onClickCreateRoom=club", roomConfig)
            UIManager.instance.showView(ViewConfig.viewConfig.ViewClubCreateTemplate, null, roomConfig)
        }
        else {
            // roomConfig.cost_type = RoomConfig.cost_type.fangzhu
            Logger.log("onClickCreateRoom=", roomConfig)
            SocketClient.req_create_room(roomConfig)
        }
    }
}
